package com.xiyue.leaspring.service.impl;

import com.xiyue.leaspring.dao.IGoodsDAO;
import com.xiyue.leaspring.dao.IItemDAO;
import com.xiyue.leaspring.dao.ITagDAO;
import com.xiyue.leaspring.service.AbstractService;
import com.xiyue.leaspring.service.IGoodsService;
import com.xiyue.leaspring.vo.Goods;
import com.xiyue.leaspring.vo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class GoodsServiceImpl extends AbstractService implements IGoodsService {

    @Autowired
    private IItemDAO iItemDAO;//注入IItemDAO数据接口实例
    @Autowired
    private ITagDAO tagDAO;//注入ITagDAO数据接口实例
    @Autowired
    private IGoodsDAO goodsDAO;//注入IGoodsDAO数据接口实例

    @Override
    public Map<String, Object> preAdd() {
        Map<String,Object> map = new HashMap<>();
        map.put("allItems",this.iItemDAO.findAll());
        map.put("allTags",this.tagDAO.findAll());
        return map;
    }

    @Override
    public boolean add(Goods vo){
        vo.setDflag(0);//新增商品信息删除标记为0
        return this.goodsDAO.save(vo).getGid()!=null;//商品保存
    }

    @Override
    public Map<String, Object> list(String keyWord, int currentPage, int lineSize) {
        Map<String,Object> map = new HashMap<>();
        Sort sort = new Sort(Sort.Direction.DESC,"gid");//降序排列
        //将分页与排序操作保存到Pageable接口对象中,后续可以通过DAO层调用方法，页数从0开始
        Pageable pageable = PageRequest.of(currentPage-1,lineSize,sort);
        if(keyWord == null || "".equals(keyWord)){//查询全部操作
            long allRecorders =  this.goodsDAO.findAllCountByDflag(0);
            map.put("allRecorders",allRecorders);//数据统计
            map.put("allGoods",this.goodsDAO.findAllByDflag(0,pageable));//数据信息
            map.put("currentPage",currentPage);//当前页
            map.put("lineSize",lineSize);//每页条数
            map.put("totalPages",allRecorders%lineSize==0?allRecorders/lineSize:(allRecorders/lineSize)+1);//总页数
        }else{//模糊查询
            long allRecorders = this.goodsDAO.getSplitCount(keyWord,0);
            List<Goods> allGoods = this.goodsDAO.findSplit(keyWord,0,pageable);
            map.put("allRecorders",allRecorders);//数据统计
            map.put("allGoods",allGoods);//数据信息
            map.put("currentPage",currentPage);//当前页
            map.put("lineSize",lineSize);//每页条数
            map.put("totalPages",allRecorders%lineSize==0?allRecorders/lineSize:(allRecorders/lineSize)+1);//每页条数
        }
        Map<Long,String> itemMap = new HashMap<>();//保存分类信息
        this.iItemDAO.findAll().forEach(item ->{
            itemMap.put(item.getIid(),item.getTitle());//保存item信息
        });
        map.put("allItems",itemMap);//商品分类
        map.put("url","/pages/back/admin/goods/list");

        return map;
    }

    @Override
    public Map<String, Object> preEdit(long id) {
        Map<String,Object> map = new HashMap<>();
        map.put("allItems",this.iItemDAO.findAll());//查询所有的商品分类
        map.put("allTags",this.tagDAO.findAll());//查询所有的商品标签
        map.put("goods",this.goodsDAO.findById(id).get());//查询指定商品信息
        map.put("goodsTags",this.goodsDAO.findTidByGoods(id));//查询指定商品拥有的标签编号
        return map;
    }

    @Override
    public boolean edit(Goods vo) {
        return this.goodsDAO.save(vo)!=null;
    }

    @Override
    public boolean remove(Set<Long> gids) {
        return this.goodsDAO.editDflag(gids,1)>0;//更新dflag字段
    }
}
